package com.ruoyi.web.controller.train;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.Farewell;
import com.ruoyi.system.service.IFarewellService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 午别Controller
 *
 * @author dp
 * @date 2023-11-27
 */
@Api(tags = {"午别"})
@RestController
@RequestMapping("/dict/farewell")
public class FarewellController extends BaseController {
    @Autowired
    private IFarewellService farewellService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询午别列表
     */
    @ApiOperation(value = "午别列表", notes = "查询午别列表详情", tags = {"午别管理"})
    //@PreAuthorize("@ss.hasPermi('dict:farewell:list')")
    @GetMapping("/list")
    public TableDataInfo list(Farewell farewell) {
        startPage();
        List<Farewell> list = farewellService.selectFarewellList(farewell);
        return getDataTable(list);
    }

    /**
     * 导出午别列表
     */
    @ApiOperation(value = "午别列表导出", notes = "导出午别列表", tags = {"午别管理"})
    //@PreAuthorize("@ss.hasPermi('dict:farewell:export')")
    @Log(title = "午别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Farewell farewell) {
        List<Farewell> list = farewellService.selectFarewellList(farewell);
        ExcelUtil<Farewell> util = new ExcelUtil<Farewell>(Farewell.class);
        util.exportExcel(response, list, "午别数据");
    }

    /**
     * 获取午别详细信息
     */
    @ApiOperation(value = "获取午别详细信息", notes = "获取午别详细信息详情", tags = {"午别管理"})
    //@PreAuthorize("@ss.hasPermi('dict:farewell:query')")
    @GetMapping(value = "/{farewellId}")
    public AjaxResult getInfo(@PathVariable("farewellId") Long farewellId) {
        return success(farewellService.selectFarewellByFarewellId(farewellId));
    }

    /**
     * 新增午别
     */
    @ApiOperation(value = "新增午别", notes = "新增午别详情", tags = {"午别管理"})
    //@PreAuthorize("@ss.hasPermi('dict:farewell:add')")
    @Log(title = "午别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody Farewell farewell) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        farewellService.completeData(farewell, userId, 1);
        return toAjax(farewellService.insertFarewell(farewell));
    }

    /**
     * 修改午别
     */
    @ApiOperation(value = "修改午别", notes = "修改午别详情", tags = {"午别管理"})
    //@PreAuthorize("@ss.hasPermi('dict:farewell:edit')")
    @Log(title = "午别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request,@RequestBody Farewell farewell) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        farewellService.completeData(farewell, userId, 2);
        return toAjax(farewellService.updateFarewell(farewell));
    }

    /**
     * 删除午别
     */
    @ApiOperation(value = "删除午别", notes = "删除午别详情", tags = {"午别管理"})
    //@PreAuthorize("@ss.hasPermi('dict:farewell:remove')")
    @Log(title = "午别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{farewellIds}")
    public AjaxResult remove(@PathVariable Long[] farewellIds) {
        return toAjax(farewellService.deleteFarewellByFarewellIds(farewellIds));
    }
}
